import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);
var bbb;
if (!window.sessionStorage.getItem('token') || window.sessionStorage.getItem('token') == '') {
    bbb = false;
} else {
    bbb = true
}
const store = new Vuex.Store({
    // 初始化时用sessionStorage.getItem('token'),这样子刷新页面就无需重新登录
    state: {
        userName: window.sessionStorage.getItem('userName'),
        token: localStorage.getItem('token'),
        isLogin: bbb, //初始时候给一个 isLogin=false 表示用户未登录
        isPassword: bbb,
    },
    mutations: {
        // 将token保存到localStorage里，token表示登陆状态
        setToken: (state, token) => {
            state.token = token;
            window.sessionStorage.setItem("token", token);
        },
        // 获取用户名
        getUser: (state, userName) => {
            //把用户名存起来
            state.userName = userName;
            window.sessionStorage.setItem("userName", userName);
        },
        //把用户登录状态存起来
        changeLogin: (state, isLogin) => {
            state.isLogin = isLogin;
            window.sessionStorage.setItem("isLogin", isLogin);
        },
        // 修改交易密码
        deal: (state, isPassword) => {
            state.isPassword = isPassword;
            window.sessionStorage.setItem("isPassword", isPassword)
        },
        // 登出
        logOut: (state) => {
            // 登出的时候要清除token
            state.token = null;
            state.userName = null;
            state.isLogin = false;
            state.isPassword = false;
            sessionStorage.removeItem("token");
            sessionStorage.removeItem("userName");
            sessionStorage.removeItem("isLogin");
            sessionStorage.removeItem("isPassword");
        }
    },
    actions: {

    }
})

export default store;